package com.jeect.gateway4.web;

import com.jeect.gateway4.util.ResponseBody;
import com.jeect.gateway4.util.ResponseCode;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.web.authentication.session.SessionAuthenticationStrategy;
import org.springframework.security.web.savedrequest.HttpSessionRequestCache;
import org.springframework.security.web.savedrequest.RequestCache;
import org.springframework.security.web.savedrequest.SavedRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
public class AuthController {
//    private SessionAuthenticationStrategy
private RequestCache requestCache = new HttpSessionRequestCache();
    @GetMapping("/auth/check")
    public ResponseBody chekc(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {//@AuthenticationPrincipal UserDetails user){
        //Authentication authentication  = SecurityContextHolder.getContext().getAuthentication();
        if(authentication!=null && authentication.isAuthenticated()){
            SavedRequest savedRequest = requestCache.getRequest(request, response);
            String url = savedRequest.getRedirectUrl();
            response.sendRedirect(url);
            return null;
        }else{
            return new ResponseBody(ResponseCode.FAILE,"未授权!");
        }
    }
}
